﻿using Microsoft.AspNetCore.Mvc;

namespace Gantt.Server.Service;

[ApiExplorerSettings(GroupName = "Gantt")]
    public partial class ProjectService : IApplicationService
    {
        private readonly SugarRepository<GanttProject> _thisRepository;
        private readonly ICurrentUser _currentUser;

        public ProjectService(SugarRepository<GanttProject> thisRepository, ICurrentUser currentUser)
        {
            _thisRepository = thisRepository;
            _currentUser = currentUser;
        }

        public async Task<RestfulResult<PageResult<GanttProject>>> GetPagesAsync(PageParam data)
        {
            var restful = new RestfulResult<PageResult<GanttProject>>() { Code = CodeStatus.OK };

            var query = await _thisRepository.AsQueryable()
                .Where(u => u.Creator == _currentUser.UserId)
                .OrderByDescending(u => u.Sort)
                .ToPageAsync(data.PageNo, data.PageSize);

            restful.Data = query;
            return restful;
        }

        public async Task<RestfulResult<PageResult<ProjectDto>>> GetlistAsync()
        {
            var restful = new RestfulResult<PageResult<ProjectDto>>() { Code = CodeStatus.OK };
            PageParam data = new PageParam { PageNo = 1, PageSize = 100 };

            var query = await _thisRepository.AsQueryable()
                .Where(u => u.IsDisable == YesOrNot.N)
                .Where(u => u.Creator == _currentUser.UserId)
                .OrderByDescending(u => u.Sort)
                .OrderByDescending(u => u.IsDefault)
                .Select(u => new ProjectDto
                {
                    Id = u.Id,
                    Code = u.Code,
                    Name = u.Name
                })
                .ToPageAsync(data.PageNo, data.PageSize);

            restful.Data = query;
            return restful;
        }

        public async Task<RestfulResult> SubmitAsync(GanttProject data)
        {
            RestfulResult restful = new() { Code = CodeStatus.BadRequest };

            if (data.Id == 0)
            {
                var isExist = await _thisRepository.IsAnyAsync(u =>
                    (u.Name == data.Name || u.Code == data.Code) &&
                    u.Creator == _currentUser.UserId);

                if (isExist)
                {
                    restful.Message = ErrorEnum.D3001.GetDescription(RuoVea.ExDto.Language.i18n.ResourceManager);
                    return restful;
                }

                bool result = await _thisRepository.InsertAsync(data);
                if (result)
                {
                    restful.Data = result;
                    restful.Code = CodeStatus.OK;
                    return restful;
                }
                restful.Message = "添加失败";
                return restful;
            }
            else
            {
                var isExist = await _thisRepository.IsAnyAsync(u =>
                    u.Id == data.Id &&
                    u.Creator == _currentUser.UserId);

                if (!isExist)
                {
                    restful.Message = ErrorEnum.D3000.GetDescription(RuoVea.ExDto.Language.i18n.ResourceManager);
                    return restful;
                }

                bool result = await _thisRepository.UpdateAsync(data);
                if (result)
                {
                    restful.Data = result;
                    restful.Code = CodeStatus.OK;
                    return restful;
                }
                restful.Message = "修改失败";
                return restful;
            }
        }

        public async Task<RestfulResult> DeleteAsync(long id)
        {
            RestfulResult restful = new() { Code = CodeStatus.BadRequest };

            if (id == 0)
            {
                restful.Message = "删除失败";
                return restful;
            }

            bool isDelete = await _thisRepository.DeleteByIdAsync(id);
            if (isDelete)
            {
                restful.Data = isDelete;
                restful.Message = "删除成功";
                restful.Code = CodeStatus.OK;
                return restful;
            }

            restful.Data = isDelete;
            restful.Message = "删除失败";
            return restful;
        }
    }
 